import 'package:sqflite/sqflite.dart';
final String tableName='table_test';
final String columnId='_id';
final String columnTitle='title';
final String columnDone='done';
class Test{
  int id;
  String title;
  bool done;
  Map<String,dynamic> toMap(){
    var map=<String,dynamic>{
      columnTitle:title,
      columnDone:done==true ? 1:0

    };
    if(id!=null){
      map[columnId]=id;
    }
    return map;
  }
  Test();
  Test.fromMap(Map<String,dynamic>map){
    id=map[columnId];
    title=map[columnTitle];
    done=map[columnDone]==1;
  }
}
class TableTestProvider{
  Database db;
  Future open(String path) async{
    db=await openDatabase(path,version:1,
      onCreate: (Database db,int version)async{
      await db.execute('''
      create table $tableName(
      $columnId integer primary key autoincrement,
      $columnTitle text not null,
      $columnDone intrger not null
      )
      ''');
      }

    );
  }
  Future<Test> insert(Test test)async{
    test.id=await db.insert(tableName, test.toMap());
    return test;
  }
  Future<Test>getTodo(int id)async{
    List<Map<String,dynamic>>maps=await db.query(tableName,
      columns: [columnId,columnDone,columnTitle],
      where: '$columnId=?',
      whereArgs: [id]
    );
    if(maps.length>0){
      return Test.fromMap(maps.first);
    }
    return Test();
  }
  Future<int>delrtr(int id)async{
    return await db.delete(tableName,where: '$columnId=?',whereArgs: [id]);
  }
  Future close()async=>db.close();
}